﻿@page "/datagrid-footer-totals"
@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore
@using System.Linq 
@inject NorthwindContext dbContext

<h1>DataGrid <strong>Footer Totals</strong></h1>

<p>This <strong>FooterTemplate</strong> allows you to display aggregated data in the columne footer.</p>

<RadzenExample Name="DataGridFooterTotals" Heading="false" Documentation="false">
<RadzenDataGrid @ref="ordersGrid" AllowFiltering="true" AllowPaging="true" PageSize="3" AllowSorting="true" 
            Data="@orders" TItem="Order">
    <Columns>
        <RadzenDataGridColumn Width="50px" TItem="Order" Title="#" Filterable="false" Sortable="false" TextAlign="TextAlign.Center">
            <Template>
                @(orders.IndexOf(context) + 1)
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Width="200px" TItem="Order" Property="OrderID" Title="Order ID">
            <FooterTemplate>
                Displayed orders: <b>@ordersGrid.View.Count()</b> of <b>@orders.Count()</b>
            </FooterTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Width="200px" TItem="Order" Property="Customer.CompanyName" Title="Customer" />
        <RadzenDataGridColumn TItem="Order" Property="Employee.LastName" Title="Employee">
            <Template Context="order">
                @order.Employee?.FirstName @order.Employee?.LastName
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="Order" Property="OrderDate" Title="Order Date" FormatString="{0:d}">
            <FooterTemplate>
                Last order date: <b>@String.Format("{0:d}", orders.OrderByDescending(o => o.OrderDate).Last().OrderDate)</b>
            </FooterTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="Order" Property="Freight" Title="Freight">
            <Template Context="order">
                @String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", order.Freight)
            </Template>
            <FooterTemplate>
                Total amount: <b>@String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", orders.Sum(o => o.Freight))</b>
            </FooterTemplate>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="Order" Property="ShipName" Title="Ship Name" />
    </Columns>
</RadzenDataGrid>
</RadzenExample>

@code {
    RadzenDataGrid<Order> ordersGrid;
    IList<Order> orders;

    protected override void OnInitialized()
    {
        orders = dbContext.Orders.Include("Customer").Include("Employee").ToList();
    }
}
